<?php

class ProfileController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function getIndex()
    {
        $user_id = 1;
        //TODO: 修改此处，根据登陆
        $nerds = Profile::find($user_id)->posts()
            ->orderBy('created_at', 'desc')
            ->paginate(5);
        $profile = Profile::find($user_id)->firstOrFail();
        $follow_count = Frelation::where('user_id', '=', $user_id)->count();
        $fans_count = Frelation::where('friend_id', '=', $user_id)->count();
        $post_count = Profile::find($user_id)->posts()->count();
        return Response::json(
            array(
                'status' => 'success',
                'state' => $nerds->toArray(),
                'profile_info' => $profile->toArray(),
                'count' => array(
                    'post_count' => $post_count,
                    'follow_count' => $follow_count,
                    'fans_count' => $fans_count,
                )
            )
        );
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function postCreate()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'username'       => 'required',
            'fullname'   => 'required',
            'profile_picture' => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        } else {
            // store
            $profile = new Profile;
            $profile->username = Input::get('username');
            $profile->fullname = Input::get('fullname');
            $profile->profile_picture = Input::get('profile_picture');
            $profile->save();
            return Response::json('success', 200); // or do a redirect with some message that file was uploaded
        }
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $user_id
     * @return Response
     */
    public function getShow($user_id)
    {
        //TODO: 修改此处，根据登陆
        $nerds = Profile::find($user_id)->posts()
            ->orderBy('created_at', 'desc')
            ->paginate(5);
        $profile = Profile::find($user_id)->firstOrFail();
        $follow_count = Frelation::where('user_id', '=', $user_id)->count();
        $fans_count = Frelation::where('friend_id', '=', $user_id)->count();
        $post_count = Profile::find($user_id)->posts()->count();
        return Response::json(
            array(
                'status' => 'success',
                'state' => $nerds->toArray(),
                'profile_info' => $profile->toArray(),
                'count' => array(
                    'post_count' => $post_count,
                    'follow_count' => $follow_count,
                    'fans_count' => $fans_count,
                )
            )
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function getEdit($id)
    {
        return Response::json(array('status' => 'success', 'state' => "edit page"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @return Response
     */
    public function postUpdate()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'user_id'   => 'required',
            'username'       => 'required',
            'fullname'   => 'required',
            'profile_picture' => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        } else {
            // store
            $profile = Profile::find(Input::get('user_id'));
            $profile->username = Input::get('username');
            $profile->fullname = Input::get('fullname');
            $profile->profile_picture = Input::get('profile_picture');
            $profile->bio = Input::get('bio');
            $profile->website = Input::get('website');
            $profile->gender = Input::get('gender');
            $profile->birthday = Input::get('birthday');
            $profile->city = Input::get('city');
            $profile->save();
            return Response::json('success', 200); // or do a redirect with some message that file was uploaded
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return Response
     */
    public function postDestroy()
    {
        $rules = array(
            'user_id'   => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        }
        // delete
        $nerd = Posts::find(Input::get('user_id'));
        $nerd->delete();

        // redirect
        return Response::json('success', 200);
    }
}